// commons/diy/cat_goods/index.js
var is_get_goods = 1;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    setting: {
      type: Object,
      value: null
    },
    headheight: {
      type: Number,
      value: 0
    },
    template: {
      type: Array,
      value: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goods_list: [],
    cat_id: 0,
    page: 1,
    cat_list: [],
    clickPitch: 0,
    loading_type: 1,
    container:null,
    header_top: 45
  },
  ready: function () {
    this.getCat();
    var template = this.properties.template;
    template.forEach(el => {
      if (el.type == "header_top") {
        this.setData({
          header_top: 84
        })
      }
    });
  },
  created: function () {
    var that = this
    setTimeout(() => {
      that.getGoodsList()
    }, 1500);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onScroll(e) {
      
    },
    showpicker: function (t) {
      this.triggerEvent('showpicker',t.currentTarget.dataset)
    },
    getCat: function (t) {
        var setting = this.properties.setting;
        var that = this;
        if (setting.all_cat == 0 ) {
          that.setData({
            cat_list: setting.list,
            cat_id: setting && setting.list ? setting.list[0].cat_id : 0
          },function () {
            that.getGoodsList()
          })
        }else {
          getApp().request({
            url: getApp().api.diy.get_cat_list,
            success: function(t) {
              that.setData({
                cat_list: t.data.list,
                cat_id: t.data.list[0].id
              },function () {
                that.getGoodsList()
              })
            },
          })
        }

    },
    
    clickPitch: function (e) {
      let i = e.currentTarget.dataset.index
      var that = this;
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        clickPitch: i,
        page: 1,
        cat_id: that.data.cat_list[i].cat_id ? that.data.cat_list[i].cat_id : that.data.cat_list[i].id,
        goods_list: [],
        loading_type: 1
      },function() {
        that.getGoodsList()
      })
    },
    listScroll: function (t) {
        console.log(t);
    },
    getGoodsList: function() {
      var that = this;
      
      if (that.data.loading_type < 0 || is_get_goods == 0) {
          return false;
      }else {
        is_get_goods = 0;
        var shop_id = wx.getStorageSync('address_info').shop_id;
        getApp().request({
          url: getApp().api.diy.get_goods_list,
          data: {
            shop_id: shop_id,
            page: that.data.page,
            cat_id: that.data.cat_id,
            is_recommend: that.data.setting.goods_type
          },
          success: function(t) {
            if (t.code == 0) {
              if (t.data.list) {
                var goods_list = that.data.goods_list;
                goods_list = goods_list.concat(t.data.list);
                that.setData({
                  goods_list: goods_list,
                  page: ++that.data.page
                })
              }else { 
                that.setData({
                  loading_type: -1
                })
              }
            }
          },
          complete: function() {
            is_get_goods = 1;
            wx.hideLoading()
          }
        })
      }
      
    }
  }
})
